/*
Copyright (c) 2007-2009, Yusuke Yamamoto
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
 * Neither the name of the Yusuke Yamamoto nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.ch_linghu.fanfoudroid.fanfou;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.util.Log;

import com.ch_linghu.fanfoudroid.http.HttpException;
import com.ch_linghu.fanfoudroid.http.Response;

/**
 * A data class representing one single status of a user.
 * 
 * @author Yusuke Yamamoto - yusuke at mac.com
 */
public class CommentStatus extends WeiboResponse implements java.io.Serializable {
	private static final long serialVersionUID = 5608030496666584608L;

 
//	private User user = null;
	
	public String commentId;
	public String uid;
	public String content;
	public Date ctime;
	public String uname;
	public String face;
	 
 
 
 
    
	/* package */CommentStatus(Response res, Weibo weibo) throws HttpException {
		super(res);
		Element elem = res.asDocument().getDocumentElement();
		init(res, elem, weibo);
	}

	/* package */CommentStatus(Response res, Element elem, Weibo weibo)
			throws HttpException {
		super(res);
		init(res, elem, weibo);
	}

	CommentStatus(Response res) throws HttpException {
		super(res);
		JSONObject json = res.asJSONObject();
		try {
			 
			 
			commentId = json.getString("comment_id");
			uid = json.getString("uid");
			content = json.getString("content");
			ctime = new Date(json.getLong("ctime"));
			uname = json.getString("uname");
			face = json.getString("face");
		 
	 
 
			
 
		} catch (JSONException je) {
			throw new HttpException(je.getMessage() + ":" + json.toString(), je);
		}

	}

	/* modify by sycheng add some field */
	public CommentStatus(JSONObject json) throws HttpException, JSONException {
		
		
		 
		commentId = json.getString("comment_id");
		uid = json.getString("uid");
		content = json.getString("content");
		ctime = new Date(json.getLong("ctime"));
		uname = json.getString("uname");
		face = json.getString("face");
 
 
	}

	public CommentStatus(String str) throws HttpException, JSONException {
		// StatusStream uses this constructor
		super();
		JSONObject json = new JSONObject(str);
		
		 
		commentId = json.getString("comment_id");
		uid = json.getString("uid");
		content = json.getString("content");
		ctime = new Date(json.getLong("ctime"));
		uname = json.getString("uname");
		face = json.getString("face");
		
		 
			
		 

		
 
	}

	private void init(Response res, Element elem, Weibo weibo)
			throws HttpException {
		//ensureRootNodeNameIs("status", elem);
//		user = new User(res, (Element) elem.getElementsByTagName("user")
//				.item(0), weibo);
		commentId = getChildText("comment_id", elem);
		uid = getChildText("uid", elem);
		content = getChildText("content", elem);
		ctime =  new Date(getChildLong("ctime", elem));
		uname = getChildText("uname", elem);
		content = getChildText("content", elem);
		face = getChildText("face", elem);
	 
		 
 
		
 
	
 
	}
 
 

	/* package */
	static List<CommentStatus> constructStatuses(Response res, Weibo weibo)
			throws HttpException {

		Document doc = res.asDocument();
		if (isRootNodeNilClasses(doc)) {
			return new ArrayList<CommentStatus>(0);
		} else {
			try {
				ensureRootNodeNameIs("statuses", doc);
				NodeList list = doc.getDocumentElement().getElementsByTagName(
						"status");
				int size = list.getLength();
				List<CommentStatus> statuses = new ArrayList<CommentStatus>(size);
				for (int i = 0; i < size; i++) {
					Element status = (Element) list.item(i);
					statuses.add(new CommentStatus(res, status, weibo));
				}
				return statuses;
			} catch (HttpException te) {
				ensureRootNodeNameIs("nil-classes", doc);
				return new ArrayList<CommentStatus>(0);
			}
		}

	}

	/* modify by sycheng add json call method */
	/* package */
	static List<CommentStatus> constructStatuses(Response res) throws HttpException {
		
		
		try {
			 
			JSONArray list = res.asJSONArray();
			Log.d("dada-json",list.toString());
			int size = list.length();
			List<CommentStatus> statuses = new ArrayList<CommentStatus>(size);
			for (int i = 0; i < size; i++) {
				statuses.add(new CommentStatus(list.getJSONObject(i)));
			}
			return statuses;
		} catch (JSONException jsone) {
			throw new HttpException(jsone);
		} catch (HttpException te) {
			throw te;
		}
	}

	public String getCommentId() {
		return commentId;
	}

	public String getUid() {
		return uid;
	}

	public String getContent() {
		return content;
	}

	public Date getCtime() {
		return ctime;
	}

	public String getUname() {
		return uname;
	}

	public String getFace() {
		return face;
	}

	@Override
	public int hashCode() {
		return commentId.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		// return obj instanceof Status && ((Status) obj).id == this.id;
		return obj instanceof CommentStatus && this.commentId.equals(((CommentStatus) obj).commentId);
	}

	@Override
	public String toString() {
 
		
		return "CommentStatus{" + "commentId=" + commentId + ", uid=" + uid + ", content='"
		+ content + '\'' + ", ctime='" + ctime + '\'' + ", uname="
		+ uname + ", face=" + face
		+'}';
	}

	public boolean isEmpty() {
		return (null == commentId);
	}
}
